#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "pdfviewer.h"
#include "library.h"

namespace Ui {
class MainWindow;
}

class Browser;
class QNetworkReply;

class MainWindow : public QMainWindow
{
	Q_OBJECT
	
public:
	explicit MainWindow(QWidget *parent = 0);
	~MainWindow();

public slots:
	void openExternalFile(QString fileName);

protected:
	void showEvent(QShowEvent *event);
	void closeEvent(QCloseEvent *event);
	
private slots:
	void openDocsLastSession();
	void autoSave();
	void setupTreeColumns();
	void populateLibraryWidget();
	void populateLibraryWidgetSearch(QList<LibrarySearchResult> results);
	void populateLibraryWidgetItem(QTreeWidgetItem *item, LibraryDoc doc);
	void setupRatingColum();
	QByteArray importFile(QString fileName, QString tags = QString());
	void importFileFromViewer(QString fileName);
	void openFile(QByteArray dochHash);
	void openFile(QString fileName, QString displayName, QByteArray dochHash, bool unimported = false);
	void on_actionScan_directory_triggered();
	void onControlsVisibleToggled(bool value);
	void onShowControls(bool value);
	void setFullScreen(bool value);
	void on_btnLibrary_toggled(bool checked);
	void on_btnReader_toggled(bool checked);
	void on_btnOptions_toggled(bool checked);
	void on_btnBrowser_toggled(bool checked);
	void on_btnImportFile_clicked();
	void on_btnImportDir_clicked();
	void on_treeLibrary_itemClicked(QTreeWidgetItem *item, int column);
	void on_treeLibrary_itemDoubleClicked(QTreeWidgetItem *item, int column);
	void on_tabPDFs_tabCloseRequested(int index);
	void on_treeLibrary_itemChanged(QTreeWidgetItem *item, int column);
	void on_btnSearchClear_clicked();
	void on_txtSearch_editingFinished();
	void onRatingChanged(QByteArray docHash, int rating);
	void onCustomContextMenuRequested(const QPoint & pos);
	void onLookupRequested();
	void onDeleteRequested();
	void onMetadataDownloaded(QString metadata);
	void updateLibraryItem(QByteArray docHash);
	void on_txtSearch_textChanged(const QString &arg1);
	void onPdfDownloaded(QByteArray contents, QString url);
	void on_btnDownloads_toggled(bool checked);
	void onNewDownloadStarting(QNetworkReply *reply, QString url, QString mime);
	void on_btnOpenExternalFile_clicked();
	void onTextNoteCreationRequested(PDFViewer *v, QByteArray docHash, int pageNumber, QPointF pos, QColor color);
	void onTextNoteDeleteRequested(PDFViewer *v, QByteArray docHash, int id);
	void onTextNoteTitleChanged(PDFViewer *v, QByteArray docHash, int id, QString title);
	void onTextNoteTextChanged(PDFViewer *v, QByteArray docHash, int id, QString text);
	void onTextNotePosChanged(PDFViewer *v, QByteArray docHash, int id, QPointF pos);
	void onTextNoteColorChanged(PDFViewer *v, QByteArray docHash, int id, QColor color);
	void onTextNoteMaximizedChanged(PDFViewer *v, QByteArray docHash, int id, bool value);
	void onTextNoteMaxSizeChanged(PDFViewer *v, QByteArray docHash, int id, QSize maxSize);
	void onReportTextChanged(PDFViewer *v, QByteArray docHash, QString text);
	void onHighlightCreationRequested(PDFViewer *v, QByteArray docHash, int pageNumber, QRectF box, QColor color);
	void onHighlightDeletionRequested(PDFViewer *v, QByteArray docHash, int pageNumber, QRectF box);
	void onInterDocumentAnchorRequested(PDFViewer *v, QByteArray docHash, int pageNumber, QPointF normalizedPos);
	void onNavigationRequested(QByteArray docHash, int pageNumber, QPointF normPos);
	void onTreeViewSortIndicatorChanged(int logicalIndex, Qt::SortOrder order);
	void restoreTreeViewSortOrder();
	void loadTreeViewSortOrder();
	void saveTreeViewSortOrder();
	bool loadAutoGit();
	void saveAutoGit();
	bool loadAutoGitDelayed();
	void saveAutoGitDelayed(bool value);
	void onTreeViewColumnResized(int logicalIndex, int oldSize, int newSize);
	void saveTreeViewColumnSizes();
	void restoreTreeViewColumnSizes();
	void onViewOffsetCHanged(QByteArray docHash, qreal viewOffsetNormalized);
	void saveShowControls(bool value);
	bool loadShowControls();
	void onDocInfoRequested(PDFViewer *v, QByteArray docHash);
	void onDocPageCacheChanged(PDFViewer *v, QByteArray docHash);
	void on_btnAbout_clicked();
	void on_checkOptionsAutoGit_toggled(bool checked);

private:
	Ui::MainWindow *ui;
	Library *library;
	Browser *browser;
	QMenu *libraryContextMenu;
	QByteArray lookupDocHash;
	QTimer *timerAutoSave;
	QList<QPair<QString, qint32> > columnSortOrder;
	bool ignoreSortOrderChanges;
	bool firstShowHandled;
};

#endif // MAINWINDOW_H
